#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include "calfunc.h"
  
int main(int argc, char** argv) {  
    int port=5555;
    int socket_descriptor; //套接口描述字  
    int iter=0;
    char buf[80];
    struct sockaddr_in address;//处理网络通信的地址
    t_cpuinfo *cpuinfo;

    bzero(&address,sizeof(address));
    address.sin_family=AF_INET;
    address.sin_addr.s_addr=inet_addr("127.0.0.1");//这里不一样  
    if(argc > 1)
        address.sin_addr.s_addr=inet_addr(argv[1]);
    address.sin_port=htons(port);
      
    socket_descriptor=socket(AF_INET,SOCK_DGRAM,0);//IPV4  SOCK_DGRAM 数据报套接字（UDP协议）  
    while(1)  
    {
        cpuinfo = get_cpu(10);
        sleep(1);
        sprintf(buf, "HOSTINFO\nCPU,%.2f,%.2f,%.2f,%.2f,%.2f",
                cpuinfo->idle,
                cpuinfo->iowait,
                cpuinfo->nice,
                cpuinfo->sys,
                cpuinfo->user);
        printf("%s\n",buf);
        sendto(socket_descriptor,buf,sizeof(buf),0,(struct sockaddr *)&address,sizeof(address));  
    }  
      
    sprintf(buf,"stop\n");    
    close(socket_descriptor);  
    printf("Messages Sent,terminating\n");  
      
    exit(0);  
        
}  